<template>
    <div class="top-category">
        <div class="container">
            <!-- 面包屑 -->
            <el-breadcrumb separator-class="el-icon-arrow-right" style="padding:25px 10px; ">
                <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
                <el-breadcrumb-item v-if="topCategory">{{topCategory.name}}</el-breadcrumb-item>
            </el-breadcrumb>
            <!-- 轮播图 -->
            <el-carousel :autoplay="true" height="500px" :interval="5000">
                <el-carousel-item v-for="item in sliders" :key="item.id">
                    <img :src="item.imgUrl">
                </el-carousel-item>
            </el-carousel>
            <!-- 所有二级分类 -->
            <div class="sub-list">
                <h3>全部分类</h3>
                <ul v-if="topCategory && topCategory.children">
                    <li v-for="item in topCategory.children" :key="item.id">
                        <a href="javascript:;">
                            <img :src="item.picture">
                            <p>{{item.name}}</p>
                        </a>
                    </li>
                </ul>
            </div>
            <!-- 分类关联商品 -->
            <div class="ref-goods" v-for="item in subList" :key="item.id">
                <div class="head">
                    <h3>- {{item.name}} -</h3>
                    <p class="tag">{{item.desc}}</p>
                    <XtxMore/>
                </div>
                <div class="body">
                    <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
                </div>
            </div>

        </div>
    </div>
</template>

<script>
    import {findBanner} from "@/api/home";
    import {findTopCategory} from "@/api/category";
    import GoodsItem from "@/views/category/components/goods-item";
    import XtxMore from "@/components/library/xtx-more";

    export default {
        name: "TopCategory",
        components: {XtxMore, GoodsItem},
        data() {
            return {
                sliders: [],
                subList:[]
            }
        },
        created() {
            this.findBanner()
            console.log(this.$route.params.id)

        },
        methods: {
            async findBanner() {
                const {result} = await findBanner()
                this.sliders = result
            },
            async findTopCategory(id) {
                const {result} = await findTopCategory(id)
                this.subList = result.children
            }
        },
        computed: {
            topCategory() {
                return this.$store.getters.categoryList.find(item => {
                    return item.id === this.$route.params.id
                })
            }
        },
        watch:{
            '$route.params.id':{
                handler(newVal){
                    if (newVal && `/category/${newVal}` === this.$route.path){
                        this.findTopCategory(newVal)
                    }
                },
                immediate:true
            }
        }
    }
</script>

<style  lang="less">
    .top-category {
        background: #f5f5f5;

        h3 {
            font-size: 28px;
            color: #666;
            font-weight: normal;
            text-align: center;
            line-height: 100px;
        }

        .sub-list {
            margin-top: 20px;
            background: #fff;

            ul {
                display: flex;
                padding: 0 32px;
                flex-wrap: wrap;

                li {
                    width: 168px;
                    height: 160px;

                    a {
                        text-align: center;
                        display: block;
                        font-size: 16px;

                        img {
                            width: 100px;
                            height: 100px;
                        }

                        p {
                            line-height: 40px;
                        }

                        &:hover {
                            color: @xtxColor;
                        }
                    }
                }
            }
        }
        .ref-goods {
            background-color: #fff;
            margin-top: 20px;
            position: relative;
            .head {
                .xtx-more {
                    position: absolute;
                    top: 20px;
                    right: 20px;
                }
                .tag {
                    text-align: center;
                    color: #999;
                    font-size: 20px;
                    position: relative;
                    top: -20px;
                }
            }
            .body {
                display: flex;
                justify-content: flex-start;
                flex-wrap: wrap;
                padding: 0 65px 30px;
            }
        }
    }


</style>